package application.client.gui.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import middleware.datacontainer.searchclient.ClientContainer;
import middleware.datacontainer.searchclient.ExemplarContainer;
import middleware.datacontainer.searchmedium.MediumContainer;
import application.client.gui.interfaces.IChoosable;
import application.controller.client.GuiController;

public class MediumInfo extends JPanel implements IChoosable{
	
	private JLabel descriptionLabel;
	private JTextField descriptionTextField;
	private JLabel yearLabel;
	private JTextField yearTextField;
	private JTextField languageTextField;
	private JLabel languageLabel;
	private JTextField departmentTextField;
	private JLabel departmentLabel;
	private JLabel typeLabel;
	private JTextField typeTextField;
	private JPanel mediumInfoPanel;
	private JLabel mediumIdLabel;
	private JTextField mediumIdTextField;
	private JPanel exemplarInfo;
	private JPanel exemplarResultPanel;
	private JScrollPane exemplarScrollPane;
	private JTable exemplarTable;
	private JPanel executeButtonPanel;
	private JButton executeLoanButton;
	private JButton executeReturnButton;
	private JButton executeExtendButton;
	private JButton executeReserveButton;
	
	private List<ExemplarContainer> tempActualMediumExemplars;
	private HashMap<Integer,ExemplarContainer> exemplars=new HashMap<Integer, ExemplarContainer>();
	
	private MediumContainer actualMedium;
	

	private int selectedRowIndexExemplarTable=-1;
	
	private String[][] exemplarDataArray=new String[][] { };
	private String[] exemplarsTableHeader=new String[] { "ID", "InventarNumber","Loan"};
	
	public MediumInfo(MediumContainer medium)
	{
		super(true);
		actualMedium=medium;
		init();
		setAllAttributes(medium);
		setButtonsVisible(GuiController.getInstance().isAUserLoggedIn());
	}
	private void init()
	{
		this.setLayout(new BorderLayout());
		this.setBackground(new java.awt.Color(255,255,255));
		
		this.add(new ImagePanel(), BorderLayout.SOUTH);
		
		{
			mediumInfoPanel = new JPanel();
			mediumInfoPanel.setLayout(null);
			this.add(mediumInfoPanel,BorderLayout.NORTH);
			mediumInfoPanel.setPreferredSize(new java.awt.Dimension(10, 220));
			mediumInfoPanel.setBackground(new java.awt.Color(255,255,255));
			mediumInfoPanel.setBorder(BorderFactory.createTitledBorder("MediumInfo"));
			
			{
				mediumIdLabel = new JLabel();
				mediumInfoPanel.add(mediumIdLabel);
				mediumIdLabel.setText("ID");
				mediumIdLabel.setBounds(40, 30, 100, 20);
				mediumIdLabel.setSize(100, 20);
			}
			{
				mediumIdTextField = new JTextField();
				mediumInfoPanel.add(mediumIdTextField);
				mediumIdTextField.setBounds(140, 30, 100, 20);
				mediumIdTextField.setEditable(false);
				mediumIdTextField.setBackground(Color.white);
			}
			{
				descriptionLabel = new JLabel();
				mediumInfoPanel.add(descriptionLabel);
				descriptionLabel.setText("Description");
				descriptionLabel.setBounds(40, 60, 100, 20);
				descriptionLabel.setSize(100, 20);
			}
			{
				descriptionTextField = new JTextField();
				mediumInfoPanel.add(descriptionTextField);
				descriptionTextField.setBounds(140, 60, 100, 20);

				descriptionTextField.setEditable(false);
				descriptionTextField.setBackground(Color.white);
			}
			{
				yearLabel = new JLabel();
				mediumInfoPanel.add(yearLabel);
				yearLabel.setText("Year");
				yearLabel.setBounds(40, 90, 100, 20);
				yearLabel.setSize(100, 20);
			}
			{
				yearTextField = new JTextField();
				mediumInfoPanel.add(yearTextField);
				yearTextField.setBounds(140, 90, 100, 20);

				yearTextField.setEditable(false);
				yearTextField.setBackground(Color.white);
			}
			{
				languageLabel = new JLabel();
				mediumInfoPanel.add(languageLabel);
				languageLabel.setText("Language");
				languageLabel.setBounds(40, 120, 100, 20);
				languageLabel.setSize(100, 20);
			}
			{
				languageTextField = new JTextField();
				mediumInfoPanel.add(languageTextField);
				languageTextField.setBounds(140, 120, 100, 20);

				languageTextField.setEditable(false);
				languageTextField.setBackground(Color.white);
			}
			{
				departmentLabel = new JLabel();
				mediumInfoPanel.add(departmentLabel);
				departmentLabel.setText("Department");
				departmentLabel.setBounds(40, 150, 100, 20);
				departmentLabel.setSize(100, 20);
			}
			{
				departmentTextField = new JTextField();
				mediumInfoPanel.add(departmentTextField);
				departmentTextField.setBounds(140, 150, 100, 20);

				departmentTextField.setEditable(false);
				departmentTextField.setBackground(Color.white);
			}
			{
				typeLabel = new JLabel();
				mediumInfoPanel.add(typeLabel);
				typeLabel.setText("Mediumtype");
				typeLabel.setBounds(40, 180, 100, 20);
				typeLabel.setSize(100, 20);
			}
			{
				typeTextField = new JTextField();
				mediumInfoPanel.add(typeTextField);
				typeTextField.setBounds(140, 180, 100, 20);

				typeTextField.setEditable(false);
				typeTextField.setBackground(Color.white);
			}
		}
		{
			exemplarInfo = new JPanel();
			BoxLayout exemplarInfoLayout = new BoxLayout(exemplarInfo, javax.swing.BoxLayout.X_AXIS);
			exemplarInfo.setLayout(exemplarInfoLayout);
			this.add(exemplarInfo, BorderLayout.CENTER);
			exemplarInfo.setBackground(new java.awt.Color(255,255,255));
			exemplarInfo.setBorder(BorderFactory.createTitledBorder("Exemplars"));
			{
				exemplarResultPanel = new JPanel();
				BoxLayout exemplarResultPanelLayout = new BoxLayout(exemplarResultPanel, javax.swing.BoxLayout.X_AXIS);
				exemplarResultPanel.setLayout(exemplarResultPanelLayout);
				exemplarInfo.add(exemplarResultPanel);
				exemplarResultPanel.setPreferredSize(new java.awt.Dimension(540, 105));
				{
					exemplarScrollPane = new JScrollPane();
					exemplarResultPanel.add(exemplarScrollPane);
					exemplarScrollPane.setPreferredSize(new java.awt.Dimension(400, 100));
					exemplarScrollPane.setSize(400, 100);
					{
						TableModel exemplarTableModel = 
								new DefaultTableModel(
										exemplarDataArray,
										exemplarsTableHeader);
						exemplarTable = new JTable();
						exemplarScrollPane.setViewportView(exemplarTable);
						exemplarTable.setModel(exemplarTableModel);
						exemplarTable.setPreferredSize(new java.awt.Dimension(400, 78));
						exemplarTable.setSize(400, 78);
					}
				}
			}
			{
				executeButtonPanel = new JPanel();
				exemplarInfo.add(executeButtonPanel);
				GridLayout executeButtonPanelLayout = new GridLayout(3, 1);
				executeButtonPanelLayout.setColumns(1);
				executeButtonPanelLayout.setHgap(2);
				executeButtonPanelLayout.setVgap(2);
				executeButtonPanelLayout.setRows(4);
				executeButtonPanel.setLayout(executeButtonPanelLayout);
				executeButtonPanel.setBackground(new java.awt.Color(255,255,255));
				executeButtonPanel.setPreferredSize(new java.awt.Dimension(205, 163));
				executeButtonPanel.setSize(205, 100);
				executeButtonPanel.setLocation(new java.awt.Point(30, 0));
				{
					executeExtendButton = new JButton();
					BoxLayout executeExtendButtonLayout = new BoxLayout(executeExtendButton, javax.swing.BoxLayout.Y_AXIS);
					executeExtendButton.setLayout(executeExtendButtonLayout);
					executeButtonPanel.add(executeExtendButton);
					executeExtendButton.setText("Extend");
					executeExtendButton.addActionListener(new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent e) {
							
							if(selectedRowIndexExemplarTable>-1)
							GuiController.getInstance().extendLoan(
									tempActualMediumExemplars.get(selectedRowIndexExemplarTable).getId());
							
						}
					});
					
				}
				{
					executeLoanButton = new JButton();
					BoxLayout executeLoanButtonLayout = new BoxLayout(executeLoanButton, javax.swing.BoxLayout.Y_AXIS);
					executeLoanButton.setLayout(executeLoanButtonLayout);
					executeButtonPanel.add(executeLoanButton);
					executeLoanButton.setText("Loan");
					executeLoanButton.setPreferredSize(new java.awt.Dimension(73, 23));
					executeLoanButton.addActionListener(new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent e) {
							selectClient(true);
						}
					});
				}
				{
					executeReserveButton = new JButton();
					BoxLayout executeReturnButtonLayout = new BoxLayout(executeReserveButton, javax.swing.BoxLayout.Y_AXIS);
					executeReserveButton.setLayout(executeReturnButtonLayout);
					executeButtonPanel.add(executeReserveButton);
					executeReserveButton.setText("Reserve");
					executeReserveButton.addActionListener(new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent e) {										
							
							selectClient(false);			
							
						}
					});
				}
				{
					executeReturnButton = new JButton();
					BoxLayout executeReturnButtonLayout = new BoxLayout(executeReturnButton, javax.swing.BoxLayout.Y_AXIS);
					executeReturnButton.setLayout(executeReturnButtonLayout);
					executeButtonPanel.add(executeReturnButton);
					executeReturnButton.setText("Return");
					executeReturnButton.addActionListener(new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent e) {
							GuiController.getInstance().returnExemplar((String) exemplarTable.getValueAt(selectedRowIndexExemplarTable, 1));
							
						}
					});
				}
				
			}
		}
	}
	public void setAllAttributes(MediumContainer mc)
	{
		descriptionTextField.setText(mc.getDescription());
		Integer year = (Integer) mc.getYear();
		yearTextField.setText(year.toString());
		
		languageTextField.setText(mc.getLanguage());
		departmentTextField.setText(mc.getDepartment());
		typeTextField.setText(mc.getType());
		mediumIdTextField.setText(mc.getId().toString());
	}
	public void setExemplars(List<ExemplarContainer> actualMediumExemplars)
	{
		tempActualMediumExemplars=actualMediumExemplars;
		exemplarDataArray = new String[actualMediumExemplars.size()][exemplarsTableHeader.length-1];
		TableModel exemplarTableModel=new DefaultTableModel(exemplarDataArray, exemplarsTableHeader);
				
		int row = 0;
		for (ExemplarContainer ex : actualMediumExemplars) {
			if(ex != null)
			{
				exemplarTableModel.setValueAt(ex.getId(), row, 0);
				exemplarTableModel.setValueAt(ex.getInvno(), row, 1);
				
				String loaned="available";
				if(ex.getLoaned())
				{
					loaned="loaned";
				}
				exemplarTableModel.setValueAt(loaned, row, 2);
				exemplars.put(row,ex);
				row++;
			}
		}
		exemplarTable.setModel(exemplarTableModel);
		exemplarTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){


			@Override
			public void valueChanged(ListSelectionEvent e) {
				  if (!e.getValueIsAdjusting()) {
					  selectedRowIndexExemplarTable=exemplarTable.getSelectedRow();
				  }
			}
		
		});
	}
	
	@Override
	public void reserveMedium(ClientContainer cC) {
		System.out.println("overrided method " + cC.getId());
		GuiController.getInstance().reserveMedium(actualMedium,cC);
	}

	@Override
	public void loan(ClientContainer cC) {
		System.out.println("orverrided method loan " + cC.getId());
		GuiController.getInstance().loan(exemplars.get(exemplarTable.getSelectedRow()), cC);
	}
	
	
	private void selectClient(boolean isItALoan)
	{
		new ClientSearchFrame(this,isItALoan);
	}
	
	@Override
	public void login(String user, String pass) {
		// TODO Auto-generated method stub
		try {
			
			throw new Exception("not implemented");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void setButtonsVisible(boolean loggedIn) {
		executeExtendButton.setEnabled(loggedIn);
		executeLoanButton.setEnabled(loggedIn);
		executeReserveButton.setEnabled(loggedIn);
		executeReturnButton.setEnabled(loggedIn);
		
	}

}
